#pragma once

#include "stdafx.h"


const enum typeCol_t {NAME, VALUE, UNIT, TYPE, STATE, DESCR, DATA, SCRIPT};
QModelIndex getIndex(const QModelIndex &old_index, typeCol_t type);
QString getComboboxDataString(QString cur_item, QStringList list);

class ParameterItem
{
public:
	ParameterItem(const QVector<QVariant> &data, ParameterItem *parent = 0);
	~ParameterItem();

	ParameterItem *child(int number);
	int childCount() const;
	int columnCount() const;
	QVariant data(int column) const;
	bool insertChildren(int position, int count, int columns);
	bool removeChildren(int position);
	ParameterItem *parent();
	int childNumber() const;
	bool setData(int column, const QVariant &value);

private:
	QList<ParameterItem*> childItems;
	QVector<QVariant> itemData;
	ParameterItem *parentItem;
};


class ParametersModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	ParametersModel(QObject *parent = 0);
	~ParametersModel();
	QVariant data(const QModelIndex &index, int role) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;

	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
	QModelIndex parent(const QModelIndex &index) const;

	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;

	Qt::ItemFlags flags(const QModelIndex &index) const;
	bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
	void addItem(const QString &script, const QString &name, const QVariant &value, const QString &units, const int &type,
		const int &state, const QString &description = "", const bool &child = false, const bool &expanded = false) const;
	void removeItems(const QString &script);
	int dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);

	QStringList getAllParameters();
	void setAllParameters(const QStringList &list);
    QString getStringParameter(const QString &name, const QString &script = "COMMON");
	double getNumParameter(const QString &name, const QString &script = "COMMON");
	double ParametersModel::getTimeVaryingParameter(const QString &name, const double &cur_time, const QString &script = "COMMON");
private:
	ParameterItem *getItem(const QModelIndex &index) const;
	QModelIndex getParameter(const QString &name, const QString &script);
	ParameterItem *rootItem;
public slots:
	void changeData(const QString &name, const QVariant &value);
	void setDescription(QSharedPointer<QLabel> descr_label, QModelIndex sel_index);
signals:
	void ItemChanged(const QString &name, const QVariant &value);
	void update();
};


class Delegate : public QItemDelegate{
	Q_OBJECT
private:		
	QWeakPointer<QTreeView> tree_widget;
public:
	Delegate(QWeakPointer<QTreeView> _tree_widget = QSharedPointer<QTreeView>(NULL)) : tree_widget(_tree_widget){ connect(this, SIGNAL(data(QString)), this, SLOT(setData(QString))); }
	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	void setEditorData(QWidget *editor, const QModelIndex &index) const;
	void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
	void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
	QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{return QSize(40,21);}
private slots:
	void paremeterEditorShow();
signals:
	void data(QString d);
	void parent_func(const QString &_name);
};